<?php
require_once("../../class2.php");
require_once("./includes/require_all.php");
$database->cyr_chars();

//precise percentage
/* $q = mysql_query("select * from admin_panel where game_show = '1'");
while($a = mysql_fetch_array($q)){
	$trans = 0;
	$all = 0;
	$qq = mysql_query("select text_id, content, english from $a[game_prefix]_text where english <> ''");
	while($aa = mysql_fetch_array($qq)){
		$bg = trim($aa['content']);
		$en = trim($aa['english']);
		$flag = preg_match("/(_IGcoLLen)/", $aa['text_id']);
		$flag2 = preg_match("/([а-яА-Я]{1,}\s?[а-яА-Я]{1,})/u", $bg);
		$temp = strlen($en);
		$all += $temp;
		if(($bg != $en) or $flag or $flag2){
			$trans += $temp;
		}
	}
	echo $a[game_name]." -- ".$trans."/".$all." -- ".round($trans*100/$all, 2)."%<br>";
} */

//Grim Tales - unapack
/*$file = "guide.xml";
$handle = fopen($file, "r");
$content = fread($handle, filesize($file));
fclose($handle);
preg_match_all("/(<li>.+<\/li>|<h2>.+<\/h2>)/", $content, $a);
$x = array();
foreach($a[0] as $v){
	$v = preg_replace("/<li>/", "", $v);
	$v = preg_replace("/<\/li>/", "", $v);
	$v = preg_replace("/<h2>/", "", $v);
	$v = preg_replace("/<\/h2>/", "", $v);
	$x[] = $v;
}
$br = 1;
foreach($x as $v){
	//echo "[".$br."] ".$v."<br>";
	$content = $database->escape_value($v);
	//$q = "insert into grimtales_text (text_id, file_name, content, english) values ($br, '$file', '$content', '$content')";
	//$database->query($q);
	$br++;
}*/

//AoM
/*$dir = "history2/";
$files = scandir($dir);
$br = 1;
foreach($files as $f){
	if($f != "." and $f != ".."){
		$file = $dir.$f;
		$handle = fopen($file, "r");
		$content = fread($handle, filesize($file));
		fclose($handle);
		$content = iconv("UCS-2LE", "UTF-8", $content);
		//$content = utf8_encode($content);
		//$content = preg_replace("/(ГїГѕ)/", "", $content);
		$x = explode("\n", $content);
		clear_array($x);
		foreach($x as $k => $v){
			//$c = $database->escape_value($v);
			//$c = addslashes(utf8_decode($v));
			$t = $k+1;
			//$q = "insert into aom_text (text_id, file_name, content, english) values ('$t', $f, $v, $v)";
			//$database->query($q);
			echo $t."=>".$v."<br>";
		}
	}
}*/

//AoM
/*ini_set("max_execution_time", 0);
$dir = "STRING/";
$files = scandir($dir);
$br = 1;
foreach($files as $f){
	if($f != "." and $f != ".."){
		$file = $dir.$f;
		$handle = fopen($file, "r");
		$content = fread($handle, filesize($file));
		fclose($handle);
		$content = iconv("cp1251", "utf-8", $content);
		$z = preg_split("/\}/", $content);
		foreach($z as $v){
			$c = explode("\n", $v);
			clear_array($c);
			$codes = array();
			$text = array();
			foreach($c as $v){
				if(preg_match("/^[0-9]{1,5},/", $v)){
					preg_match("/^[0-9]{1,5}/", $v, $x);
					$codes[] = $x[0];
					$y = preg_split("/\"/", $v);
					array_shift($y);
					clear_array($y);
					$text[] = implode("\"", $y);
				}
			}
			for($i=0;$i<count($text);$i++){
				$t = $database->escape_value(stripslashes(htmlspecialchars($text[$i])));
				//echo $br." => ".$f." => ".$codes[$i]." => ".$t."<br>";
				//$q = "insert into aom_text (text_id, file_name, content, english) values (".$codes[$i].", '".$f."', '".$t."', '".$t."')";
				//$database->query($q);
				$br++;
			}
		}
	}
}
ini_set("max_execution_time", 30);*/

//SAW
/*$dir = "INT/";
$files = scandir($dir);
foreach($files as $f){
	if($f != "." and $f != ".."){
		$br = 1;
		$file = $dir.$f;
		$handle = fopen($file, "r");
		$content = fread($handle, filesize($file));
		fclose($handle);
		$content = iconv("UCS2", "UTF-8", $content);
		//$z = preg_split("/\}/", $content);
		$z = explode("\n", $content);
		foreach($z as $v){
			if(preg_match("/\(Text=/", $v)){
				//echo $v."<br>";
				$c = explode("Text=", $v);
				$y = substr($c[1], 1, -3);
				//clear_array($c);
				$t = $database->escape_value($y);
				//echo $f." => ".$br." => ".$t."<br>";
				$q = "insert into saw_text (text_id, file_name, content, english) values (".$br.", '".$f."', '".$t."', '".$t."')";
				$database->query($q);
			}
			$br++;
		}
	}
}

$file = "SawGame.int";
$handle = fopen($file, "r");
$content = fread($handle, filesize($file));
fclose($handle);

$content = iconv("UCS2", "UTF-8", $content);
$z = explode("\n", $content);
$text_id = array();
$content = array();
$br = 1;
foreach($z as $v){
	if(preg_match("/(=)/", $v) and !preg_match("/(Name=\")/", $v)){
		$c = explode("=", $v);
		$text_id[] = $database->escape_value($c[0])."_".$br;
		$content[] = $database->escape_value($c[1]);
	}
	$br++;
}

foreach($text_id as $k => $v){
	//echo $file." => ".$v." => ".$content[$k]."<br>";
	$q = "insert into saw_text (text_id, file_name, content, english) values ('".$v."', '".$file."', '".$content[$k]."', '".$content[$k]."')";
	$database->query($q);
}*/

//Batman
/*$dir = "INT/";
$files = scandir($dir);
foreach($files as $f){
	if($f != "." and $f != ".."){
		$br = 1;
		$file = $dir.$f;
		$handle = fopen($file, "r");
		$content = fread($handle, filesize($file));
		fclose($handle);
		$content = iconv("UCS2", "UTF-8", $content);
		$x = explode("\n", $content);
		//clear_array($x);
		foreach($x as $k => $v){
			if(preg_match("/=/", $v)){
				$y = explode("=", $v);
				$text_id = $database->escape_value(array_shift($y)."_".$br);
				$content = $database->escape_value(implode($y));
				//echo $text_id." => ".$content."<br>";
				//$q = "insert into batman_text (text_id, file_name, content, english) values ('".$text_id."', '".$f."', '".$content."', '".$content."')";
				//$database->query($q);
			}
			$br++;
		}
		//echo "<pre>".print_r($x, true)."</pre>";
	}
}

$dir = "asd/";
$files = scandir($dir);
foreach($files as $f){
	if($f != "." and $f != ".."){
		$br = 1;
		
		$file = $dir.$f;
		$handle = fopen($file, "r");
		$content = fread($handle, filesize($file));
		fclose($handle);
		$content = strhex($content);

		$x = str_split($content, 2);
		
		$start_308 = $x[309].$x[310].$x[311];
		$start_336 = $x[337].$x[338].$x[339];
		$start_364 = $x[365].$x[366].$x[367];
		
		if($start_308 == "000000" and $x[308] != "01"){
			$len = hexdec($x[308]);
				
			$text = "";
			for($i=308;$i<312+$len;$i++){
				$text .= $x[$i];
			}
		}
		elseif($start_336 == "000000" and $x[336] != "01"){
			$len = hexdec($x[336]);
				
			$text = "";
			for($i=336;$i<340+$len;$i++){
				$text .= $x[$i];
			}
		}
		elseif($start_364 == "000000" and $x[364] != "01"){
			$len = hexdec($x[364]);
				
			$text = "";
			for($i=364;$i<368+$len;$i++){
				$text .= $x[$i];
			}
		}
		
		$y = implode("", $x);

		preg_match_all("/$text/i", $y, $x);
		
		foreach($x[0] as $v){
			$t = hexstr(substr($v, 4));
			if(strlen($t)>2){
				$t = $database->escape_value($t);
				//echo "[".$br."] ".$f." => ".$t."<br>";
				$q = "insert into batman_text_2 (text_id, file_name, content, english) values ('$br', '$f', '$t', '$t')";
				$database->query($q);
				$br++;
			}
		}
	}
}*/

//Born Into Darkness - unpack
/*$file = "strings.xml";
$handle = fopen($file, "r");
$content = fread($handle, filesize($file));
fclose($handle);

$z = array();
$x = preg_split("/(String\">)/", $content);
array_shift($x);
foreach($x as $v){
	$y = explode("<", $v);
	$z[] = $y[0];
}

$text_id = array();
$text = array();
foreach($z as $k => $v){
	if($k%2 == 1){
		$text[] = $v;
	}
	else{
		$text_id[] = $v;
	}
}

foreach($text_id as $k => $v){
	//echo $v." => ".$text[$k]."<br>";
	//$q = "insert into bid_text (text_id, file_name, content, english) values ('".$database->escape_value($v)."', '$file', '".$database->escape_value($text[$k])."', '".$database->escape_value($text[$k])."')";
	//$database->query($q);
}*/

//Born Into Darkness - pack

/*$file = "strings.xml";
$handle = fopen($file, "r");
$content = fread($handle, filesize($file));
fclose($handle);

$q = "select * from bid_text";
$r = $database->query($q);
while($a = $database->fetch_array($r)){
	$eng[] = htmlspecialchars_decode($a['english'], ENT_NOQUOTES);
	$bg[] = htmlspecialchars(str_replace("\n", "", trim($a['content'])), ENT_NOQUOTES);
}

foreach($eng as $k => $v){
	$content = str_replace(">".$v."<", ">".$bg[$k]."<", $content);
	echo $v." => ".$bg[$k]."<br>";
}

$f = "strings_bg.xml";
$h = fopen($f, "w+");
fwrite($h, $content);
fclose($h);*/

//Mystery of the Ancients  Lockwood - unpack
/*$file = "guide.xml";
$handle = fopen($file, "r");
$content = fread($handle, filesize($file));
fclose($handle);
preg_match_all("/(<li>.+<\/li>|<h2>.+<\/h2>)/", $content, $a);
foreach($a[0] as $v){
	$v = preg_replace("/<li>/", "", $v);
	$v = preg_replace("/<\/li>/", "", $v);
	$v = preg_replace("/<h2>/", "", $v);
	$v = preg_replace("/<\/h2>/", "", $v);
	$x[] = $v;
}
$br = 1;
foreach($x as $v){
	//echo "[".$br."] ".$v."<br>";
	$content = $database->escape_value($v);
	$q = "insert into mal_text (text_id, file_name, content, english) values ($br, '$file', '$content', '$content')";
	$database->query($q);
	$br++;
}

$file = "strings.xml";
$handle = fopen($file, "r");
$content = fread($handle, filesize($file));
fclose($handle);

$x = explode("\n", $content);

foreach($x as $k => $v){
	if(preg_match("/(Data=)/", $v)){
		$y = explode("Data=\"", $v);
		$id = $database->escape_value(trim(str_replace("<", "", $y[0])));
		$text = $database->escape_value(str_replace("\" />", "", $y[1]));
		//echo "[".$id."] ".$text."<br>";
		$q = "insert into mal_text (text_id, file_name, content, english) values ('$id', '$file', '$text', '$text')";
		$database->query($q);
	}
}*/

//Mystery of the Ancients  Lockwood - pack
/*$file = "guide.xml";
$handle = fopen($file, "r");
$content = fread($handle, filesize($file));
fclose($handle);

$r = $database->query("select * from mal_text where file_name = '$file'");
while($a = $database->fetch_array($r)){
	//$eng = str_replace("\n", "", trim($a['english']));
	$con = str_replace("\n", "", trim($a['content']));
	$content = str_replace(">".$a['english']."<", ">".$con."<", $content);
}

$f = "guide_bg.xml";
$h = fopen($f, "w+");
fwrite($h, $content);
fclose($h);

$file = "strings.xml";
$handle = fopen($file, "r");
$content = fread($handle, filesize($file));
fclose($handle);

$r = $database->query("select * from mal_text where file_name = '$file'");
while($a = $database->fetch_array($r)){
	//$eng = str_replace("\n", "", trim($a['english']));
	$con = str_replace("\n", "", trim($a['content']));
	$content = str_replace("\"".$a['english']."\"", "\"".$con."\"", $content);
}

$f = "strings_bg.xml";
$h = fopen($f, "w+");
fwrite($h, $content);
fclose($h);*/

//Strange Cases 3 - The Secrets of Grey Mist Lake - unpack
/*$file = "strings.xml";
$handle = fopen($file, "r");
$content = fread($handle, filesize($file));
fclose($handle);

$x = explode("\n", $content);
foreach($x as $k => $v){
	if(preg_match("/(Type=\"String\">)/", $v)){
		$y = explode("Type=\"String\">", $v);
		$z = explode("<", $y[1]);
		$text[] = $z[0];
	}
}

foreach($text as $k => $v){
	if($k%2==1){
		$con[] = $v;
	}
	else{
		$id[] = $v;
	}
}

foreach($id as $k => $v){
	//echo "[".$v."] ".$con[$k]."<br>";
	$con[$k] = str_replace("'", "\\'", $con[$k]);
	$database->query("insert into sc3_text (text_id, file_name, content, english) values ('$v', '$file', '$con[$k]', '$con[$k]')");
}*/

//Far Cry - unpack
/*$dir = "Far Cry/";
$files = scandir($dir);
$br = 1;
foreach($files as $f){
	if($f != "." and $f != ".."){
		$content = file_get_contents($dir.$f);
		$x = explode("\n", $content);
		$all = array();
		foreach($x as $k => $v){
			if(preg_match("/(<Data ss:Type=\"String\">.+<\/Data><\/Cell>)/", $v)){
				$all[] = $v;
			}
		}
		foreach($all as $k => $v){
			if(preg_match("/(<Cell ss:StyleID=\"s21\"><Data ss:Type=\"String\">)(Key|English|Foreign Language|Foreign)(<\/Data><\/Cell>)/", $v)){
				unset($all[$k]);
			}
		}
		$new = array();
		foreach($all as $k => $v){
			$x = explode("String\">", $v);
			$y = explode("<", $x[1]);
			$z = trim($y[0]);
			if(strlen($z) > 0){
				$new[] = $z;
			}
		}
		$id = array();
		$text = array();
		foreach($new as $k => $v){
			if($k == 0){
				$id[] = $v;
			}
			elseif($k % 2 == 1){
				$text[] = $v;
			}
			else{
				$id[] = $v;
			}
		}
		foreach($id as $k => $v){
			$i = $database->escape_value($v);
			$t = $database->escape_value($text[$k]);
			//$database->query("insert into farcry_text (text_id, file_name, content, english) values ('$i', '$f', '$t', '$t')");
			//echo "[".$br."] ".$i."=>".$t."<br>";
			//$br++;
		}
	}
}*/

//Diablo II - pack
/* function sort_text($text, $max_len){
	$new_text = "";
	$add = "абвгдежзийклмнопрстуфхцчшщьъюяАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЬЪЮЯ0123456789\'\",.\?!=-%@#$^&*()[]{}:;\\|<>\/`~\x83\xbf\x82\xa9";
	$all = str_word_count($text, 1, $add);
	$len_count = 0;
	foreach($all as $k => $v){
		if(preg_match("/[а-яА-Я]+/u", $v)){
			$word_len = strlen($v)/2;
		}
		else{
			$word_len = strlen($v);
		}
		$len_count += $word_len + 1;
		if($len_count > $max_len){
			$new_text .= "\n".$v." ";
			$len_count = $word_len + 1;
		}
		else{
			$new_text .= $v." ";
		}
	}
	return $new_text;
}

$q = mysql_query("select distinct file_name from diablo2_text");
while($a = mysql_fetch_array($q)){
	$files[] = $a['file_name'];
}

foreach($files as $file){
	$text = "Key	Value\n";
	$q = mysql_query("select * from diablo2_text where file_name = '$file'");
	while($a = mysql_fetch_array($q)){
		$id = preg_replace("/(_IGcoLLen)/", "", preg_replace("/(_[0-9]+)/", "", $a['text_id']));
		$content = str_replace("\n", "", trim($a['content']));
		// $content = str_replace("\n", "\\n", $content);
		$content = !empty($content) ? $content : " ";
		$text .= $id."\t\"".trim($content)."\"\n";
	}
	file_put_contents($file, $text);
} */

//Medal of Honor - unpack
/*$dir = "INT/";
$files = scandir($dir);
$br = 1;
foreach($files as $f){
	if($f != "." and $f != ".."){
		$content = file_get_contents($dir.$f);
		$x = explode("\n", $content);
		foreach($x as $k => $v){
			if(preg_match("/=/", $v)){
				$y = explode("=", $v);
				$i = $database->escape_value(array_shift($y))."_$br";
				$t = $database->escape_value(implode("=", $y));
				$database->query("insert into moh_text (text_id, file_name, content, english) values ('$i', '$f', '$t', '$t')");
				$br++;
				//echo $id."=>".$text."<br>";
			}
		}
	}
}*/

//Alan Wake - unpack

/*$file = "string_table.bin.txt";
$content = file_get_contents($file);
$x = explode("\n", $content);
array_pop($x);
foreach($x as $k => $v){
	$y = explode("=", $v);
	$id = $database->escape_value($y[0]);
	$text = $database->escape_value($y[1]);
	//echo $id."=>".$text."<br>";
	mysql_query("insert into alanwake_text (text_id, file_name, content, english) values ('$id', '$file', '$text', '$text')");
}*/

/*$file = "prevod.txt";
$content = file_get_contents($file);
$x = explode("\n", $content);
$text = "";
for($i=0;$i<318;$i++){
	$y = explode("=", $x[$i]);
	$text = $database->escape_value($y[1]);
	$c = $i+1;
	mysql_query("update alanwake_text set content = '$text', translator = 'Mnementh' where id = '$c'");
}*/

//////////////////////
//					//
//	Skyrim - UNPACK	//
//					//
//////////////////////

/*//HEADER
//uint32 - number of strings
//uint32 - size of strings
//
//DIRECTORY ENTRY
//uint32 - id
//uint32 - offset after header and directory
//
//STRING DATA
//	if .STRINGS
//	string - string
//	if .DLSTRINGS, .ILSTRINGS
//	uint32 - string size
//	char[string size] - string

$file = "Skyrim_English.STRINGS";
$ext_temp = explode(".", $file);
$name = $ext_temp[0];
$ext = $ext_temp[1];
$content = file_get_contents($file);

//HEADER
$strings_num_temp = unpack("V", substr($content, 0, 4));
$strings_num = $strings_num_temp[1];
$strings_size_temp = unpack("V", substr($content, 4, 4));
$strings_size = $strings_size_temp[1];

//DIRECTORY ENTRY
$off_temp = 8;
for($i=0;$i<$strings_num;$i++){
	$id_temp = unpack("V", substr($content, $off_temp, 4));
	$id[] = $id_temp[1];
	$off_temp += 4;
	$strings_offset_temp = unpack("V", substr($content, $off_temp, 4));
	$strings_offset[] = $strings_offset_temp[1];
	$off_temp += 4;
}

//STRING DATA
$text = "";
if($ext == "STRINGS"){
	$last_off_key = count($strings_offset) - 1;
	foreach($strings_offset as $k => $v){
		if($k == $last_off_key){
			$string_len = $strings_size - $v;
		}
		else{
			$string_len = $strings_offset[$k+1] - $v;
		}
		$string = substr($content, $off_temp, $string_len);
		$text .= $id[$k]."=>".$string."\n";
		$off_temp += $string_len;
	}
}

file_put_contents($name.".txt", $text);*/

//igi pack
/*$dir = "english/";
$files = scandir($dir);
foreach($files as $f){
	if($f != "." and $f != ".."){
		$content = file_get_contents($dir.$f);
		$q = mysql_query("select * from igi_text where file_name = '$f'");
		while($a = mysql_fetch_array($q)){
			//$a['content'] = trim($a['content']);
			$content = "=>".str_replace($a['english']."=", "=>".$a['content']."=", $content);
		}
		file_put_contents($dir.$f, $content);
	}
}*/

//secret files 2 pack
/*$dir = "tunguska/";
$files = scandir($dir);
foreach($files as $f){
	if($f != "." and $f != ".."){
		$content = file_get_contents($dir.$f);
		$x = explode("\n", $content);
		$head = array_shift($x);
		foreach($x as $k => $v){
			$y = explode("\t", $v);
			$q = mysql_query("select content from secretfiles2_text where text_id = '$y[0]' and file_name = '$f' limit 1");
			$a = mysql_fetch_array($q);
			$y[1] = trim($a['content']);
			$z = implode("\t", $y);
			$x[$k] = $z;
		}
		$new = implode("\n", $x);
		$new = iconv("UCS2", "UTF8", $new);
		//$new = array_unshift($head);
		file_put_contents("tunguska_bg/".$f, $new);
	}
}*/

//stupid invaders - unpack
/*$all = array();
$text = "";
$dir = "Cod/";
$files = scandir($dir);
foreach($files as $f){
	if($f != "." and $f != ".."){
		$content = file_get_contents($dir.$f);
		$x = explode("\x06\x00\x00\x00", $content);
		array_shift($x);
		
		foreach($x as $v){
			$y = strpos($v, "\x00");
			$str = substr($v, 0, $y);
			if(!in_array($str, $all)){
				$all[] = $str;
			}
		}
	}
}

foreach($all as $v){
	$v = str_replace("\n", "\\n", $v);
	$text .= $v."=>\n";
}
file_put_contents("si.txt", $text);*/

//strange cases 3 - pack
/* $content = file_get_contents("strings_en.xml");
$q = mysql_query("select * from sc3_text where text_id not like '%_IGcoLLen'");
while($a = mysql_fetch_array($q)){
	$eng = $a['english'];
	$bg = trim($a['content']);
	$bg = htmlspecialchars_decode($bg);
	$bg = htmlspecialchars($bg);
	$content = str_replace(">".$eng."<", ">".$bg."<", $content);
}

file_put_contents("strings_bg.xml", $content); */

//max mayne 2 - unpack
/*$dir = "subtitles/";
$files = scandir($dir);
$br = 1;
foreach($files as $f){
	if($f != "." and $f != ".."){
		$content = file_get_contents($dir.$f);
		$x = explode("\n", $content);
		
		foreach($x as $r){
			if(preg_match("/(String = )/", $r)){
				if(preg_match("/(\/\/)/", $r)){
					$y = explode("//String = \"", $r);
					$t = explode("\"", $y[1]);
					$text = $t[0];
				}
				else{
					$y = explode("String = \"", $r);
					$t = explode("\"", $y[1]);
					$text = $t[0];
				}
				//echo $br."=>".$text."<br>";
				$text = $database->escape_value($text);
				mysql_query("insert into maxpayne2_text (text_id, file_name, content, english) values ($br, '$f', '$text', '$text')");
				$br++;
			}
		}
	}
}*/

//stupid invaders - pack
/*$content = file_get_contents("si.txt");
$dir = "Cod/";
$x = explode("\n", $content);
foreach($x as $v){
	$y = explode("=>", $v);
	$eng = $y[0];
	$bg = $y[1];
	
	if($bg){
		$eng = str_replace("\\n", "\x0a", $eng);
		$bg = str_replace("\\n", "\x0a", $bg);
		$len_eng = strlen($eng);
		$len_bg = strlen($bg);
		$byte_eng = 4 - (($len_eng + 1) % 4);
		if($byte_eng == 4) $byte_eng = 0;
		$byte_bg = 4 - (($len_bg + 1) % 4);
		if($byte_bg == 4) $byte_bg = 0;
		$files = scandir($dir);
		foreach($files as $f){
			$c = file_get_contents($dir.$f);
			$c = str_replace($eng."\x00".str_repeat("X", $byte_eng), $bg."\x00".str_repeat("X", $byte_bg), $c);
			file_put_contents($dir.$f, $c);
		}
	}
}*/

//knigths and merchants - unpack
/*$dir = "text.txt";
$content = file_get_contents($dir);

$x = explode("\n", $content);
array_pop($x);

$br = 1;
foreach($x as $v){
	if($v){
		$str = $database->escape_value($v);
		//echo $br." => ".$str."<br>";
		mysql_query("insert into knm_text (text_id, file_name, content, english) values ($br, '$dir', '$str', '$str')");
		$br++;
	}
}*/

//ps move heroes - pack
/*$content = file_get_contents("game.nlu");

$x = explode("\n", $content);
$str = "";
foreach($x as $v){
	$y = explode(" ", $v);
	$id = $y[0];
	$id2 = $y[0]."_IGcoLLen";
	$q = mysql_query("select * from moveheroes_text where text_id = '$id' or text_id = '$id2'");
	$a = mysql_fetch_array($q);
	$text = $database->escape_value(trim($a['content']));
	$v = $id." ".$text;
	$str .= $v."\n";
}
echo $str;
//file_put_contents("game_bg.nlu", $str);*/

//saw - pack
/*$dir = "saw/";
$files = scandir($dir);
foreach($files as $f){
	if($f != "." and $f != ".."){
		$content = file_get_contents($dir.$f);
		if(preg_match("/(A_)/", $f)){
			$q = mysql_query("select * from saw_text where file_name = '$f'");
			while($a = mysql_fetch_array($q)){
				$eng = htmlspecialchars_decode($a['english']);
				$bg = trim($a['content']);
				$content = str_replace("=\"$eng\"", "=\"$bg\"", $content);
			}			
		}
		else{
			$q = mysql_query("select * from saw_text where file_name = '$f'");
			while($a = mysql_fetch_array($q)){
				$eng = htmlspecialchars_decode($a['english']);
				$bg = trim($a['content']);
				$content = str_replace("=$eng", "=$bg", $content);
			}			
		}
		file_put_contents("saw_bg/".$f, $content);
	}
}*/

//homeworld 2 - unpack

/*function find_all_files($dir){
	$root = scandir($dir);
	foreach($root as $value)
	{
		if($value === '.' || $value === '..') {continue;}
		if(is_file("$dir/$value")) {$result[]="$dir/$value";continue;}
		foreach(find_all_files("$dir/$value") as $value)
		{
			$result[]=$value;
		}
	}
	return $result;
}

$dir = "locale";
$files = find_all_files($dir);

foreach($files as $f){
	$info = pathinfo($f);
	$dir = $info['basename'];
	$content = iconv("UCS2", "UTF-8", file_get_contents($f));
	$x = explode("\r\n", $content);
	foreach($x as $v){
		if(strlen($v) > 0){
			$y = explode("	", $v);
			$id = $database->escape_value($y[0]);
			$str = $database->escape_value($y[1]);
			//echo $id." => ".$str."<br>";
			mysql_query("insert into hw2_text (text_id, file_name, content, english) values ('$id', '$dir', '$str', '$str')");
		}
	}
}*/

//githic - pack
/*$c = file_get_contents("gothic 3/stringtable.ini");
$q = mysql_query("select * from gothic_text where text_id not like '%_IGcoLLen' and english <> ''");
while($a = mysql_fetch_array($q)){
	$en = $a['english'];
	$bg = preg_replace("/[\r\n]/", "", trim($a['content']));
	$c = str_replace("=".$en.";", "=".$bg.";", $c);
	//echo $a['text_id']." => ".$bg."<br>";
}

file_put_contents("stringtable_bg.ini", $c);*/

//monkey island 1 - unpack
/*$content = file_get_contents("speech_niki.info.txt");
$x = explode("\n", $content);
$br = 0;
$text = "";
foreach($x as $k => $v){
	//qvka - 0:596; nikeq - 1517:2070
	if(preg_match("/[а-яА-Я]{3,}/u", $v)){
		if($br < 595 or ($br > 1516 and $br < 2069)){
			//echo $x[$k+2]."=>".$x[$k-3]."=>".$v."<br>";
			$text .= $x[$k+2]."=>".$x[$k-3]."=>".$v."\n";
		}
		else{
			//echo $x[$k+2]."=>".$x[$k-3]."=><br>";
			$text .= $x[$k+2]."=>".$x[$k-3]."=>\n";
		}
		$br++;
	}
}
file_put_contents("speech_niki.info_.txt", $text);*/

/*$content = file_get_contents("speech_niki.info_.txt");
$x = explode("\n", $content);
foreach($x as $k => $v){
	$y = explode("=>", $v);
	$id = $y[0];
	$en = $database->escape_value($y[1]);
	$bg = $database->escape_value($y[2]);
	//echo "[".$id."] ".$en." => ".$bg."<br>";
	if(empty($bg)){
		mysql_query("insert into mi_text (text_id, file_name, content, english) values ('$id', 'speech_niki.info.txt', '$en', '$en')");
	}
	else{
		mysql_query("insert into mi_text (text_id, file_name, content, english) values ('$id', 'speech_niki.info.txt', '$bg', '$en')");
	}
}*/

//voyage - unpack
/*$content = file_get_contents("JV4.txt");
$x = explode("\n", $content);
array_pop($x);
array_shift($x);
foreach($x as $k => $v){
	$y = explode("=>", $v);
	$id = $y[0];
	$en = $database->escape_value($y[2]);
	//echo "[".$id."] ".$en."<br>";
	mysql_query("insert into voyage_text (text_id, file_name, content, english) values ('$id', 'JV4.txt', '$en', '$en')");
}*/

//knights and merchants - pack
/*$c = file_get_contents("text.txt");
$q = mysql_query("select * from knm_text where file_name = 'text.txt'");
while($a = mysql_fetch_array($q)){
	$en = $a['english'];
	$bg = trim($a['content']);
	$c = str_replace($en, $bg, $c);
}
file_put_contents("text_bg.txt", $c);*/

//resident evil 5 - pack

/*$dir = "Resident Evil 5 Original files/";
$files = scandir($dir);
foreach($files as $f){
	if($f != "." and $f != ".."){
		$content = file_get_contents($dir.$f);
		$x = explode("\n", $content);
		$q = mysql_query("select * from re5_text");
		while($a = mysql_fetch_array($q)){
			if(!preg_match("/(_IGcoLLen/", $a['text_id'])){
				$en = $a['english'];
				$bg = $a['content'];
				if(preg_match("/\{(.+,.+)\}/", $bg)){
					preg_match("/\{(.+,.+)\}/", $bg, $chunk);
					foreach($chunk as $cc){
						$ccc = str_replace(", ", ",", $cc);
						$bg = str_replace($cc, $ccc, $bg);
					}
				}
				foreach($x as $k => $v){
					if($v == $en){
						$x[$k] = trim($bg);
					}
				}
			}
		}
		$xx = implode("\n", $x);
		file_put_contents("re5_bg/".$f, $xx);
	}
}*/

//far cry - pack

/* $a = "asd (dsa)";
if(preg_match("/".preg_quote($a)."/", $a)){
	echo "DA";
}
else{
	echo "NE";
} */
/* $dir = "Far Cry/";
$files = scandir($dir);
foreach($files as $f){
	if($f != "." and $f != ".."){
		$c = file_get_contents($dir.$f);
		$c = str_replace("Вэл", "Val", $c);
		$c = str_replace("Джек", "Jack", $c);
		$c = str_replace("Дойл", "Doyle", $c);
		$q = mysql_query("select * from farcry_text where file_name = '$f'");
		while($a = mysql_fetch_array($q)){
			$bg = trim($a['content']);
			$en = $a['english'];
			if(preg_match("/<\/Cell>\n    (<Cell ss:StyleID=\".+\">|<Cell>)<Data ss:Type=\"String\">(".preg_quote($en).")<\/Data><\/Cell>\n    (<Cell ss:StyleID=\".+\">|<Cell>)<Data ss:Type=\"(.+)\">(.+)<\/Data><\/Cell>/u", $c)){
				$c = preg_replace("/<\/Cell>\n    (<Cell ss:StyleID=\".+\">|<Cell>)<Data ss:Type=\"String\">(".preg_quote($en).")<\/Data><\/Cell>\n    (<Cell ss:StyleID=\".+\">|<Cell>)<Data ss:Type=\"String\">(.+)<\/Data><\/Cell>/u", "</Cell>\n    $1<Data ss:Type=\"String\">$2</Data></Cell>\n    $3<Data ss:Type=\"String\">$bg</Data></Cell>", $c);
			}
		}
		file_put_contents("fc_bg/".$f, $c);
	}
} */

//darkest of days

/*$file = iconv("UCS-2", "UTF-8", file_get_contents("language.csv"));
$x = explode("\r", $file);
foreach($x as $v){
	$v = str_replace("\n", " ", $v);
	if(preg_match('/(\",\")/', $v)){
		$y = explode("\",\"", $v);
		$z = explode("\"", $y[0]);
		$id = $database->escape_value($z[1]);
		$t = explode("\"", $y[1]);
		$text = $database->escape_value($t[0]);
		//echo $id." => ".$text."<br><br>";
		//mysql_query("insert into dod_text (text_id, file_name, content, english) values ('$id', 'language.csv', '$text', '$text')");
	}
}*/

//update

/*$qq = mysql_query("select * from admin_panel where game_name != 'GTA 4' and game_percent != '100'");
while($aa = mysql_fetch_array($qq)){
	
	$gamename = $aa['game_name'];
	
	//Counting all rows for translation, all rows with cyrillic chars in them and percent
	$q = "SELECT * FROM ".$aa['game_prefix']."_text WHERE content <> ''";
	$r = mysql_query($q);
	$all = mysql_num_rows($r); //Counter for all english rows for translation
	$all_cyr = 0; //Counter for all cyrillic rows that are already translated
	while($a = mysql_fetch_array($r)){
		$content = $a['content'];
		$text_id = $a['text_id'];
		if(preg_match("/[а-яА-Я]{2,}/u", $content) OR preg_match("/(_IGcoLLen){1}/", $text_id)){
			$all_cyr++;
		}
	}
	$percent = round(($all_cyr*100)/$all, 2);
	
	$translation_status = $database->fetch_array($database->query("select date, game_percent, increase from admin_panel where game_name = '$gamename'"));
	$perc = floatval(str_replace(",", ".", $translation_status['game_percent']));
	$p = $percent - $perc;
	$increase = floatval(str_replace(",", ".", $translation_status['increase']));
	$post_time = $translation_status['date'];
	$year = substr($post_time, 0, 4);
	$month = substr($post_time, 5, 2);
	$day = substr($post_time, 8, 2);
	$date = mktime(00, 00, 01, $month, $day, $year);
	$seconds = time() - $date;
	$days = floor($seconds/84600);

	if($days == 0){
		$increase += $p;
		$query = "UPDATE admin_panel SET rows_translated = $all_cyr, rows_all = $all, game_percent = '$percent', date = NOW(), increase = '$increase' WHERE game_name = '$gamename'";
		$database->query($query);
	}
	else{
		$query = "UPDATE admin_panel SET rows_translated = $all_cyr, rows_all = $all, game_percent = '$percent', date = NOW(), increase = '$p' WHERE game_name = '$gamename'";
		$database->query($query);
	}
}*/

//sh4 - unpack

/*$file_name = "message_building0_e.bin";
$file = file_get_contents($file_name);
$header_temp = unpack("V", substr($file, 0, 4));
$header_1 = $header_temp[1];
$header_temp = unpack("V", substr($file, 4, 4));
$header_2 = $header_temp[1];
$files_temp = unpack("v", substr($file, $header_2, 2));
$files = $files_temp[1];
$off = $header_2 + 2;
$file_size = filesize($file_name);
for($i=0;$i<$files;$i++){
	$temp = unpack("v", substr($file, $off, 2));
	$start = $temp[1];
	$off += 2;
	$next_temp = unpack("v", substr($file, $off, 2));
	$next = $next_temp[1];
	if($i == $files - 1){
		$len = ($file_size - ($start*2 + $header_2))/2;
	}
	else{
		$len = $next - $start;
	}
	$all[(string)$start] = $len;
}

foreach($all as $k => $v){
	$text = array();
	$start = intval($k)*2 + $header_2;
	$len = $v*2;
	$s = substr($file, $start, $len);
	$q = preg_split("/(\xFF\xFF)/", substr($s, 2));
	for($a=0;$a<count($q)-1;$a++){
		$text[] = $q[$a];
	}
	foreach($text as $value){
		for($j=0;$j<strlen($value);$j++){
			$x = unpack("C", $value[$j]);
			$y = $x[1]-1;
			if($y == -1){
				$y = 223;
			}
			elseif($y == 254){
				$y = 245;
			}
			$z = ~$y;
			$char = pack("C", $z);
			$char = str_replace("\n", "&lt;br&gt;", $char);
			echo $char;
		}
	}
	echo "<br>";
}*/

//gta4 - replace
/*$q = mysql_query("select * from gta4_text");
$all = array();
$z = array();
while($a = mysql_fetch_array($q)){
	$en = $a['english'];
	$id = $a['text_id'];
	if(preg_match("/(~.+~)/", $en)){
		preg_match_all("/(~[A-Za-z0-9]{1}~)|(~[A-Z0-9_]{2,}~)/", $en, $all);
		array_shift($all);
		//echo $id." =>";
		foreach($all as $v){
			foreach($v as $vv){
				//echo $vv;
				if(!in_array($vv, $z)){
					$z[] = $vv;
				}
			}
		}
		//echo "<=<br>";
	}
}
$text = "";
foreach($z as $k => $v){
	if($v){
		$text .= "<".$k.">"."=>".$v."\n";
	}
}
file_put_contents("gta4_replace.txt", $text);*/

/*$c = file_get_contents("gta4_replace.txt");
$x = explode("\n", $c);
foreach($x as $v){
	if($v){
		$y = explode("=>", $v);
		$q = mysql_query("select * from gta4_text where english like '%$y[1]%'");
		while($a = mysql_fetch_array($q)){
			$id = $a['id'];
			$bg = trim($a['content']);
			$en = trim($a['english']);
			$bg = mysql_real_escape_string(str_replace($y[1], $y[0], $bg));
			$en = mysql_real_escape_string(str_replace($y[1], $y[0], $en));
			mysql_query("update gta4_text set content = '$bg', english = '$en' where id = $id");
		}
	}
}*/

//shenmue - unpack

/*$dir = "trans/";
$files = scandir($dir);
$br = 1;
$text = "";
foreach($files as $f){
	if($f != "." and $f != ".."){
		$c = file_get_contents($dir.$f);
		$x = explode("</subtitle>", $c);
		array_pop($x);
		foreach($x as $v){
			$y = substr(strrchr($v, ">"), 1);
			//echo $y."<br>";
			//mysql_query("insert into shenmue_text (text_id, file_name, content, english) values ('".$br."_".$dir.$f."', 'shenmue.txt', '".$database->escape_value($y)."', '".$database->escape_value($y)."')");
			$br++;
			$text .= $y."\n";
		}
	}
}
file_put_contents("shenmue_translated.txt", $text);*/

//Tom Clancy

/* $file = "Oasis_English.inf";
$c = file_get_contents($file);
$x = explode("\n", $c);
array_shift($x);
array_shift($x);
array_shift($x);
array_shift($x);
$br = 1;
foreach($x as $v){
	$y = explode(":", $v);
	array_shift($y);
	array_shift($y);
	array_shift($y);
	$new = implode(":", $y);
	$text = $database->escape_value($new);
	mysql_query("insert into tomclancy_text (text_id, file_name, content, english) values ($br, '$file', '$text', '$text')");
	//echo $br." -- ".$new."<br>";
	$br++;
} */

//prison break

/* $dir = "Prison Break - The Conspiracy [BG in progress]/";
$dir2 = "Prison Break - The Conspiracy [BG in progress]/en/";
$files = scandir($dir);
foreach($files as $f){
	if($f != '.' and $f != '..' and $f != 'en'){
		$br = 1;
		$c = file_get_contents($dir.$f);
		$c2 = file_get_contents($dir2.$f);
		$x = explode("\n", $c);
		$x2 = explode("\n", $c2);
		foreach($x as $k => $v){
			if($v){
				//echo "[".$br."] ".$v." -- ".$x2[$k]."<br>";
				$bg = $database->escape_value($v);
				$en = $database->escape_value($x2[$k]);
				mysql_query("insert into prisonbreak_text (text_id, file_name, content, english) values ($br, '$f', '$bg', '$en')");
			}
			$br++;
		}
	}
} */

//prison break - pack

/* $dir = "Prison Break - The Conspiracy [BG in progress]/bg/";
$q = mysql_query("select distinct(file_name) from prisonbreak_text");
$files = array();
while($a = mysql_fetch_array($q)){
	$files[] = $a['file_name'];
}

foreach($files as $f){
	$text = "";
	$qq = mysql_query("select content from prisonbreak_text where file_name = binary '$f'");
	while($aa = mysql_fetch_array($qq)){
		$text .= trim($aa['content'])."\n";
	}
	file_put_contents($dir.$f, $text);
} */

//assassin creed 2 - unpack

/* $dir = "assassin creed 2/";
$files = scandir($dir);
foreach($files as $f){
	if($f != "." and $f != ".."){
		$c = file_get_contents($dir.$f);
		$x = explode("\n", $c);
		clear_array($x);
		$br = 1;
		foreach($x as $k => $v){
			if($v and $k % 3 == 1){
				//echo $br." -- ".htmlspecialchars($v)."<br>";
				mysql_query("insert into ac2_text (text_id, file_name, content, english) values ('$br', '$f', '".$database->escape_value($v)."', '".$database->escape_value($v)."')");
				$br++;
			}
		}
	}
} */

//ac2 - pack
// ini_set("max_execution_time", -1);
// $dir = "assassin creed 2/";
// $files = scandir($dir);
// foreach($files as $f){
	// if($f != "." and $f != ".."){
		// $c = file_get_contents($dir.$f);
		// $q = mysql_query("select * from ac2_text where file_name = '$f'");
		// while($a = mysql_fetch_array($q)){
			// $bg = trim($a['content']);
			// $en = htmlspecialchars_decode($a['english']);
			// $c = str_replace("]\n".$en."\n\n[", "]\n".$bg."\n\n[", $c);
		// }
		// // preg_match_all("/(\]\n.+\n\n\[)/", $c, $all);
		// // print_r($all);
		// file_put_contents($dir."bg_".$f, $c);
	// }
// }

//batman - pack

/* $dir = "INT/";
$files = scandir($dir);
foreach($files as $f){
	if($f != "." and $f != ".."){
		$c = file_get_contents($dir.$f);
		$c = mb_convert_encoding($c, "utf-8", "ucs-2le");
		$q = mysql_query("select * from batman_text where file_name = '$f'");
		while($a = mysql_fetch_array($q)){
			$bg = trim($a['content']);
			$en = $a['english'];
			$c = str_replace("=".$en."\n", "=".$bg."\n", $c);
		}
		$c = mb_convert_encoding($c, "ucs-2le", "utf-8");
		file_put_contents("INT_bg/".$f, $c);
	}
} */

//batman - dialogues fix

/* function find_all_files($dir){
	$root = scandir($dir);
	foreach($root as $value)
	{
		if($value === '.' || $value === '..') {continue;}
		if(is_file("$dir/$value")) {$result[]="$dir/$value";continue;}
		foreach(find_all_files("$dir/$value") as $value)
		{
			$result[]=$value;
		}
	}
	return $result;
}

$dir = "unpacked";
$files = find_all_files($dir);
foreach($files as $file){
	$q = mysql_query("select * from batman_text where file_name like '%sound%'");
	while($a = mysql_fetch_array($q)){
		$str_en = $a['english'];
		$len_en = strlen($str_en) + 1;
		$str_bg = trim($a['content']);
		$len_bg = mb_strlen($str_bg, "utf-8") + 1;
		$c = file_get_contents($file);
		if(strstr($c, $str_en)){
			// $x = preg_split("/(.{4}".$str_en.".{1})/", $c);
			$x = explode($str_en, $c);
			$bg = mb_convert_encoding($str_bg, "windows-1251", "utf-8");
			// echo unpack("H*", $bg)[1];
			$bg_text = pack("V", $len_bg).$bg;
			$en_norm_text = pack("V", $len_en).$str_en;
			if($len_en == $len_bg){
				$en_after_bg_text = $en_norm_text;
			}
			elseif($len_en > $len_bg){
				$diff = $len_en - $len_bg;
				$en_after_bg_text = pack("V", $len_en + $diff).$str_en.str_repeat(" ", $diff);
			}
			else{
				$diff = $len_bg - $len_en;
				$en_after_bg_text = pack("V", $len_en - $diff).substr($str_en, 0, -$diff);
			}
			$new_text = '';
			foreach($x as $k => $v){
				if($k == 1){
					$new_text .= $v.$bg_text;
				}
				elseif($k == 2){
					$new_text .= $v.$en_after_bg_text;
				}
				elseif($k == count($x) - 1){
					$new_text .= $v;
				}
				else{
					$new_text .= $v.$en_norm_text;
				}
			}
			file_put_contents($file, $new_text);
		}
	}
} */

//saw - new text
/* $file = "saw_new_text.txt";
$c = file_get_contents($file);
$x = explode("\n", $c);
$br = 1;
foreach($x as $v){
	if($v){
		// echo $v."<br>";
		$t = mysql_real_escape_string($v);
		mysql_query("insert into saw_text (text_id, file_name, content, english) values (".$br.", '".$file."', '".$t."', '".$t."')");
		$br++;
	}
} */

//mi:se - pack

/* $file = "speach.info";
$c = file_get_contents($file);
$q = mysql_query("select * from mi_text where translator = 'original' or translator = 'purpleisdead' or translator = 'guybrush'");
while($a = mysql_fetch_array($q)){
	$en = $a['english'];
	$bg = mb_convert_encoding($a['content'], "windows-1251", "utf-8");
	// $bg = $a['content'];
	$new_en = $en."\x00".str_repeat(" ", 256 - strlen($en) - 1);
	$new_bg = $bg."\x00".str_repeat(" ", 256 - strlen($bg) - 1);
	$c = str_replace($new_en, $new_bg, $c);
	echo $new_en." [".strlen($en)."] -- ".$new_bg."<br>";
}
file_put_contents("speach_new.info", $c); */

//alan wake - pack

/* $q = mysql_query("select * from alanwake_text");
$text = '';
while($a = mysql_fetch_array($q)){
	$t .= trim(str_replace("_IGcoLLen", "", $a['text_id']))."=".trim($a['content'])."\n";
}
file_put_contents("string_table.bin_bg.txt", $t); */

//rome total war - ids changing

/* $q = mysql_query("select * from rome_text");
while($a = mysql_fetch_array($q))
{
	$text_id = $a['text_id'];
	$id = $a['id'];
	if(preg_match("/(.)+(_IGcoLLen)+/", $text_id))
	{
		$text_id = preg_replace("/(.)+(_IGcoLLen)+/", "$id$2", $text_id);
		mysql_query("update rome_text set text_id = '$text_id' where id = '$id'");		
	}
	else
	{
		mysql_query("update rome_text set text_id = '$id' where id = '$id'");		
	}
} */

// batman - additional text

/* $file = "batman_additional_text.txt";
$c = file_get_contents($file);
$x = explode("\n", $c);

$br = 1;
foreach($x as $xx)
{
	$t = mysql_real_escape_string($xx);
	// echo $br." -- ".$t."<br>";
	mysql_query("insert into batman_text (text_id, file_name, content, english) values ('".$br."', '".$file."', '".$t."', '".$t."')");
	$br++;
} */

/*$q = "SELECT * FROM rome_text WHERE english <> ''";
$qq = $database->query($q);
while ($a = $database->fetch_array($qq)) {
	$en = mysql_real_escape_string($a["english"]);
	$bg = mysql_real_escape_string($a["content"]);

	$newQ = "UPDATE rometotalwar_text SET content = '{$bg}' WHERE english = '{$en}' AND content <> '{$bg}'";
	$newQQ = $database->query($newQ);
}*/
?>